from threading import Thread,Lock
import time

tickets = [f'ticket{i}' for i in range(100)]


class SubTread(Thread) :
    def run(self):
        mutex.acquire()
        time.sleep(5)
        t_name,total = self.buy()
        print('我是线程 %s 购买成功 %s，剩余%d 张'% (self.name ,t_name,total))
        mutex.release()

    def buy(self):
        global tickets
        return tickets.pop(),len(tickets)

if __name__ == '__main__' :
    mutex = Lock()
    t = [SubTread() for i in range(10)]

    for i in t :
        i.start()

    for i in t:
        i.join()

    print('所有购票结束')


